package com.yeeiee.system.domain.vo;

import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.annotation.Nullable;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.springframework.http.HttpStatus;

/**
 * <p>
 * 响应视图
 * </p>
 *
 * @author chen
 * @since 2024-04-27
 */
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Schema(title = "R", description = "响应视图")
public final class R<T> {

    @Schema(title = "是否成功")
    private boolean success;

    @Nullable
    @Schema(title = "数据")
    private T data;

    @Schema(title = "状态码")
    private int code;

    @Nullable
    @Schema(title = "消息")
    private String message;

    public static R<Void> ok() {
        return ok(null);
    }

    public static <T> R<T> ok(T data) {
        return new R<>(true, data, 200, null);
    }

    public static R<Void> error(HttpStatus httpStatus, Exception e) {
        return error(httpStatus.value(), ExceptionUtils.getRootCauseMessage(e));
    }

    public static R<Void> error(HttpStatus httpStatus, String message) {
        return error(httpStatus.value(), message);
    }

    public static R<Void> error(int code, String message) {
        return new R<>(false, null, code, message);
    }
}
